package org.jsrml.core.purchaser.service;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.jsrml.common.persistence.BaseDao;
import org.jsrml.core.purchaser.command.RemoveDeptCo;
import org.jsrml.core.purchaser.command.SaveDepartmentCo;
import org.jsrml.core.purchaser.entity.Company;
import org.jsrml.core.purchaser.entity.Department;
import org.jsrml.core.purchaser.exception.PurchaserException;
import org.jsrml.core.purchaser.qo.DepartmentQO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class DepartmentService extends BaseDao<Department, DepartmentQO>{

	@Autowired
	private CompanyService companyService;
	

	public Department createDepartment(SaveDepartmentCo command) throws PurchaserException{
		
		/**
		 * 查找部门对应的公司
		 */
		checkCode(command.getDepartmentCode());
		Company company = null;
		Department parentDept = null;
		if(StringUtils.isNotBlank(command.getCompanyId())){
			company = companyService.get(command.getCompanyId());
		}
		
		/**
		 * 查找部门对应的上级科室
		 */
		if(StringUtils.isNotBlank(command.getParentDeptId())){
			parentDept = get(command.getParentDeptId());
		}
		
		Department department = new Department();
		department.create(command, parentDept, company);
		save(department);
		
		return department;
	}
	
	public Department modifyDepartment(SaveDepartmentCo command) throws PurchaserException{
		
		if(StringUtils.isBlank(command.getDepartmentId())){
			throw new PurchaserException(
					PurchaserException.NECESSARY_PARAM_NULL, "缺少必需的参数!");
		}
		
		DepartmentQO qo = new DepartmentQO();
		qo.setId(command.getDepartmentId());
		qo.setWriteLock(true);
		qo.setFetchParentDept(true);
		qo.setFetchCompany(true);
		Department department = queryUnique(qo);
		if (department == null) {
			throw new PurchaserException(
					PurchaserException.DEPARTMENT_NOT_FOUND, "部门没有找到!");
		}
		/**
		 * 验证部门是否被修改
		 */
		Company company = null;
		Department parentDept = null;
		if(StringUtils.isNotBlank(command.getCompanyId())){
			company = companyService.get(command.getCompanyId());
		}
		
		if(StringUtils.isNotBlank(command.getParentDeptId())){
			parentDept = get(command.getParentDeptId());
		}
		
		department.modify(command, parentDept, company);
		
		return department;
	}


	public void removeDepartment(RemoveDeptCo command) throws PurchaserException {
		
		if(StringUtils.isBlank(command.getDepartmentId())){
			throw new PurchaserException(
					PurchaserException.NECESSARY_PARAM_NULL, "缺少必需的参数!");
		}
		Department department = get(command.getDepartmentId());
		if (department == null) {
			throw new PurchaserException(
					PurchaserException.DEPARTMENT_NOT_FOUND, "部门没有找到!");
		}
		department.remove();
	}
	
	@Transactional(rollbackFor = PurchaserException.class)
	public void plRemoveDept(RemoveDeptCo command) throws PurchaserException{
		if(command.getDepartmentIds()==null || command.getDepartmentIds().size()==0){
			throw new PurchaserException(
					PurchaserException.NECESSARY_PARAM_NULL, "缺少必需的参数!");
		}
		
		for(String deptId : command.getDepartmentIds()){
			Department department = get(deptId);
			if (department == null) {
				throw new PurchaserException(
						PurchaserException.DEPARTMENT_NOT_FOUND, "部门没有找到!");
			}
			
			department.getStatus().setRemove(true);
			
			save(department);
		}
		
	}
	

	private void checkCode(String code) throws PurchaserException{
		if(StringUtils.isBlank(code)){
			throw new PurchaserException(
					PurchaserException.NECESSARY_PARAM_NULL, "缺少必需的参数!");
		}
		DepartmentQO qo = new DepartmentQO();
		qo.setCode(code);
		Department department = queryUnique(qo);
		if(department!=null){
			throw new PurchaserException(
					PurchaserException.ENTITY_CODE_FOUND, "部门编码重复!");
		}
	}

	@Override
	protected Criteria buildCriteria(Criteria criteria, DepartmentQO qo) {
		return criteria;
	}

	@Override
	protected Class<Department> getEntityClass() {
		return Department.class;
	}

	
}
